using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;
using Mpi.Common.Collections;
using Xid.Controls.ParameterBrowserCtrls;
using Xeml.Document.Contracts;
using Xid.Controls.ParameterEditCtrls;

namespace Xid.Controls
{
    public partial class VariableTermEditCtrl : UserControl
    {

        #region Fields
        private List<VariableTermContextEditCtrl> contextCtrlPool = new List<VariableTermContextEditCtrl>();

        #endregion

        public VariableTermEditCtrl()
        {
            InitializeComponent();
            kryptonPanelContextCtrls.Controls.Clear();
            IncCtrlPool(3);
            //this.parameterTreeCtrl.SelectedParamTreeNodeChanged += new EventHandler(OnParamTreeNodeSelected);
            //this.parameterTreeCtrl.ParameterPopulated += new EventHandler(OnParameterTreeParameterPopulated);
            //this.parameterTreeCtrl.ParameterDePopulated += new EventHandler(OnParameterTreeParameterDePopulated);
      
        }

        #region Methods

        /// <summary>
        /// Increases the pool of context edit controls.
        /// </summary>
        /// <param name="poolSize">The total number of controls.</param>
        private void IncCtrlPool(int poolSize)
        {
            if (this.contextCtrlPool.Count < poolSize)
            {
                kryptonPanelContextCtrls.SuspendLayout();

                for (int i = this.contextCtrlPool.Count; i < poolSize; i++)
                {

                    VariableTermContextEditCtrl cec = new VariableTermContextEditCtrl();
                    cec.Name = "CEC_" + i;
                    kryptonPanelContextCtrls.Controls.Add(cec);
                    cec.MinimumSize = new Size(250, 0);
                    cec.MaximumSize = new Size(250, 0);
                    cec.Margin = new Padding(0, 0, 0, 20);
                    if (contextCtrlPool.Count > 0)
                        cec.Location = new Point(contextCtrlPool[contextCtrlPool.Count - 1].Right + 2, 5);
                    else
                        cec.Location = new Point(2, 5);
                    cec.SetupSpec(null, null, null, Color.White);
                    contextCtrlPool.Add(cec);
                }
                kryptonPanelContextCtrls.ResumeLayout();
            }
        }





        public void NavigateTo(Term p, IVariableOntologyTerm supvis)
        {

            VariableTerm dt = p as VariableTerm;
            //foreach (ContextEditCtrl cec in this.contextCtrlPool)
            //{
            //    cec.SetupSpec(null, null, null, Color.White);
            //}
            IncCtrlPool(supvis.ContextCount);
            for (int i = 0; i < contextCtrlPool.Count; i++)
            {
                contextCtrlPool[i].Visible = i < supvis.ContextCount;
                contextCtrlPool[i].Enabled = i < supvis.ContextCount;
            }

            string[] contexts = supvis.Contexts;
            for (int i = 0; i < contexts.Length; i++)
            {
                contextCtrlPool[i].SetupSpec(supvis, contexts[i], supvis.Prototype.NameSpaceAlias, ContextColorMgr.Instance.Map(contexts[i]));
                if (p != null)
                {
                    contextCtrlPool[i].Term = dt;
                    contextCtrlPool[i].SetupValue(dt.Values.Find(contexts[i]), false);
                }
                else
                {
                    contextCtrlPool[i].Term = null;
                }
            }
            this.kryptonHeaderValueTimeNav.Values.Heading = supvis.Prototype.Name;
            this.kryptonHeaderValueTimeNav.Values.Description = supvis.Prototype.NameSpaceAlias + ":" + supvis.TermId;
         }

        #endregion



       
    }
}
